﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Fish_Atlantis.ProfessorPanel
{
  public partial class ProfessorMP : System.Web.UI.MasterPage
  {
      static public string username = "用户";
      protected void Page_Load(object sender, EventArgs e)
      {
          if (Session["UserID"] == null && !check_cookie())//登录时保存在Session的值 
          {
             // Response.Write("<script language='javascript'>alert('您还未登录，请登录后再操作！');window.location.href='Login.aspx' ;</script>");
              Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('您还未登录，请登录后再操作！！');location.href='/Account/Login.aspx';</script>");
          }
          else
          {
              username = Session["UserName"].ToString();
              string nowUserType = Helper.ExecuteScalar("select User_Type from Users where UserID = '" + Session["UserID"] + "'");//从Users表获取用户ID对应的用户Type
              if (nowUserType != "3")
              {
                  Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('操作失败！无权限访问Professor页面！请切换Professor账号登陆！');location.href='/Account/Login.aspx';</script>");
              }
          }
      }
      protected void GetTypeName(object sender, EventArgs e)
      {
          if (Response.Cookies["UserNameCookie"] != null)
          {
              HttpCookie myCookie = new HttpCookie("UserNameCookie");
              myCookie.Expires = DateTime.Now.AddDays(-1d);
              Response.Cookies.Add(myCookie);
          }
          if (Response.Cookies["UserPasswordCookie"] != null)
          {
              HttpCookie myCookie = new HttpCookie("UserPasswordCookie");
              myCookie.Expires = DateTime.Now.AddDays(-1d);
              Response.Cookies.Add(myCookie);
          }
          Session["UserID"] = null;
          Session["UserName"] = null;
          Session["User_Type"] = null;
          Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('注销成功！');location.href='/Account/Login.aspx';</script>");
      }
      protected bool check_cookie()
      {

          HttpCookie UserNameCookie = Request.Cookies["UserNameCookie"];
          HttpCookie UserPasswordCookie = Request.Cookies["UserPasswordCookie"];
          if (UserNameCookie != null && UserPasswordCookie != null)
          {
              string str = "select * from Users where UserName='" + UserNameCookie.Values["UserName"].ToString() + "' and PassWord='" + UserPasswordCookie.Values["UserPassword"].ToString() + "'";

              DataSet ds = Helper.GetDataSet(str, "Users");
              if (ds.Tables[0].Rows.Count <= 0)
              {
                  return false;
              }
              else
              {
                  Session["UserID"] = ds.Tables[0].Rows[0]["UserID"].ToString();
                  Session["UserName"] = ds.Tables[0].Rows[0]["UserName"].ToString();
                  Session["User_Type"] = ds.Tables[0].Rows[0]["User_Type"].ToString();
                  username = Session["UserName"].ToString();
                  return true;
              }
          }
          return false;
      }
  }
}
